package com.alibaba.cloud.ai.example.chat.openai.configuration;

import org.apache.http.client.HttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;

import java.time.Duration;

/**
 * @author steel
 * @datetime 2025/2/8 14:16
 */
@Configuration(proxyBeanMethods = false)
public class RestCustomAutoConfiguration {

    @Bean
    public RestTemplate restTemplate() {
        try {
            ClientHttpRequestFactory factory = createRequestFactory();
            return new RestTemplate(factory);
        } catch (Exception e) {
            throw new RuntimeException("Failed to create RestTemplate", e);
        }
    }

    private ClientHttpRequestFactory createRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        // 设置连接超时时间（单位：毫秒）
        factory.setConnectTimeout(5000);
        // 设置读取超时时间（单位：毫秒）
        factory.setReadTimeout(5000);
        return factory;
    }

    @Bean
    public WebClient webClient() {
        return WebClient.create();
    }
}
